﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ParieurPro.DAL;

namespace MyBet.Web.Controls
{
    public partial class Match : System.Web.UI.UserControl
    {

        /// <summary>
        /// Mode des matchs :
        /// Readonly : pari clos
        /// Edit : pari ouvert
        /// </summary>
        public enum MatchMode
        {
            Readonly,
            Edit
        }

        #region Attributes
        private int _matchId;
            private String _homeTeam;
            private String _homeScore;
            private String _awayTeam;
            private String _awayScore;
            private MatchMode _mode;
            private String _homeBetScore;
            private String _awayBetScore;
        #endregion

        #region Properties

            public int MatchId
            {
                get { return _matchId; }
                set { _matchId = value; }
            }

            public MatchMode Mode
            {
                get { return _mode; }
                set 
                { 
                    _mode = value;
                    switch (_mode)
                    {
                        case MatchMode.Readonly:
                            EventReadonly.Visible = true;
                            EventEdit.Visible = false;
                            break;
                        case MatchMode.Edit:
                            EventReadonly.Visible = false;
                            EventEdit.Visible = true;
                            break;
                        default:
                            throw new Exception("Unknown mode");
                    }
                }
            }

            public String HomeTeam
            {
                get { return _homeTeam; }
                set 
                {
                    _homeTeam = value;
                    switch (Mode)
                    {
                        case MatchMode.Readonly:
                            HomeTeamReadOnly.Text = _homeTeam;
                            break;
                        case MatchMode.Edit:
                            HomeTeamEdit.Text = _homeTeam;
                            break;
                        default:
                            throw new NotImplementedException("Unknown mode");
                    }
                }
            }

            public String HomeScore
            {
                get { return _homeScore; }
                set
                {
                    _homeScore = value;
                    switch (Mode)
                    {
                        case MatchMode.Readonly:
                            HomeScoreReadOnly.Text = _homeScore;
                            break;
                        case MatchMode.Edit:
                            HomeScoreEdit.Text = _homeScore;
                            break;
                        default:
                            throw new NotImplementedException("Unknown mode");
                    }
                }
            }

            public String AwayScore
            {
                get { return _awayScore; }
                set
                {
                    _awayScore = value;
                    switch (Mode)
                    {
                        case MatchMode.Readonly:
                            AwayScoreReadOnly.Text = _awayScore;
                            break;
                        case MatchMode.Edit:
                            AwayScoreReadOnly.Text = _awayScore;
                            break;
                        default:
                            throw new NotImplementedException("Unknown mode");
                    }
                }
            }

            public String AwayTeam
            {
                get { return _awayTeam; }
                set
                {
                    _awayTeam = value;
                    switch (Mode)
                    {
                        case MatchMode.Readonly:
                            AwayTeamReadOnly.Text = _awayTeam;
                            break;
                        case MatchMode.Edit:
                            AwayTeamEdit.Text = _awayTeam;
                            break;
                        default:
                            throw new NotImplementedException("Unknown mode");
                    }
                }
            }

            public String HomeBetScore
            {
                get { return _homeBetScore; }
                set
                {
                    _homeBetScore = value;
                    switch (Mode)
                    {
                        case MatchMode.Readonly:
                            break;
                        case MatchMode.Edit:
                            HomeScoreEdit.SelectedValue = _homeBetScore;
                            break;
                        default:
                            throw new NotImplementedException("Unknown mode");
                    }
                }
            }

            public String AwayBetScore
            {
                get { return _awayBetScore; }
                set
                {
                    _awayBetScore = value;
                    switch (Mode)
                    {
                        case MatchMode.Readonly:
                            break;
                        case MatchMode.Edit:
                            AwayScoreEdit.SelectedValue = _awayBetScore;
                            break;
                        default:
                            throw new NotImplementedException("Unknown mode");
                    }
                }
            }

            public string HomeNewBetScore 
            {
                get { return _homeScore; }
            }

            public string AwayNewBetScore
            {
                get { return _awayScore; }
            }      

        #endregion

            public void SetMatch(MatchObject match, MatchMode mode)
            {
                Dictionary<int,string> teamsDictionary=Session["TeamsDictionary"] as Dictionary<int,string>;
                if (teamsDictionary == null)
                {
                    throw new Exception("Teams dictionary is not loaded");
                }

                this.Mode = mode;
                HomeTeam = teamsDictionary[match.TeamHomeId];
                AwayTeam = teamsDictionary[match.TeamAwayId];
                MatchId = match.MatchId;

                if (match.TeamHomeScore.HasValue)
                {
                    HomeScore = match.TeamHomeScore.Value.ToString();
                }
                else
                {
                    HomeScore = "?";
                }

                if (match.TeamAwayScore.HasValue)
                {
                    AwayScore = match.TeamAwayScore.Value.ToString();
                }
                else
                {
                    AwayScore = "?";
                }
                if (match.TeamHomeBetScore.HasValue)
                {
                    HomeBetScore = match.TeamHomeBetScore.ToString();
                }
                if (match.TeamAwayBetScore.HasValue)
                {
                    AwayBetScore = match.TeamAwayBetScore.ToString();
                }
            }

        /// <summary>
        /// Renvoi vrai si le match a un pari valide
        /// </summary>
        /// <returns></returns>
            public bool IsBetValid()
            {
                return ((this.HomeScoreEdit.SelectedValue != "-1") && (this.AwayScoreEdit.SelectedValue != "-1"));
            }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void HomeScoreEdit_SelectedIndexChanged(object sender, EventArgs e)
        {
            _homeScore = this.HomeScoreEdit.SelectedValue;
        }

        protected void AwayScoreEdit_SelectedIndexChanged(object sender, EventArgs e)
        {
            _awayScore = this.AwayScoreEdit.SelectedValue;
        }

    }
}